<%= render :partial => 'spree/admin/shared/js_head' %>

<% content_for :page_title do %>
  <%= t('spree.active_sale.title.edit') %> &#62; <%= @active_sale.name %>
<% end %>

<% content_for :page_actions do %>
  <li>
    <%= button_link_to t('spree.active_sale.link.back'), collection_url, :icon => 'icon-arrow-left', :id => 'admin_new_active_sale_link' %>
  </li>
<% end %>

<div id="ajax_error" class="errorExplanation" style="display:none;"></div>

<fieldset class="no-border-top">
  <div>
    <%= label_tag nil, Spree.t(:tree) %><br />
    <div id="active_sale_tree" class="tree" data-url="<%= admin_active_sale_active_sale_events_path @active_sale %>"></div>
  </div>
  <div id="progress" style="display:none;">
    <%= image_tag 'spinner.gif', :title => 'Spinner', :style => "vertical-align:bottom;" %> <%= Spree.t(:updating) %>..
  </div>

  <p class='hint info'>
    <%= t('spree.active_sale.event.active_sale_tree_hint') %>
  </p>

  <br>
</fieldset>

<% content_for :head do %>
  <%= javascript_tag do -%>
    var initial = [
      { "attr" :
      { "id" : "<%= @active_sale.root.try(:id) %>", "rel" : "root", "parent_id" : "nil" },
      "data" : "<%= escape_javascript(raw(@active_sale.root.try(:name))) %>",
      "state" : "open",
      "children" : [
        <% unless @active_sale.root.nil? %>
          <% @active_sale.root.children_sorted_by_position.each_with_index do |event,i| %>
            {
              "attr" :
              { "id" : "<%= event.id %>", "parent_id" : "<%= event.parent_id %>" },
              "data" : "<%= escape_javascript(raw(event_js_data(event))) %>"
              <% unless event.children.empty? %>
                ,"state" : "closed"
              <% end %>
            }<%= ',' if i < (@active_sale.root.children.size - 1) %>
          <% end %>
        <% end %>
      ]
        }
    ];
  <% end -%>
<% end %>
